package app

import (
	"gitee.com/tangrx/go-admin/utils/config"
	"github.com/gin-gonic/gin"
	"github.com/lunny/log"
	"net/http"
	"time"
)

var (
	// BeeApp is an application instance
	engine *gin.Engine
)

func init() {
	engine = gin.Default()
	engine.Use(Logger(3 * time.Second)) // 日志中间件
	engine.Use(CORSMiddleware())        // 允许跨域
	engine.Use(TokenMiddleware())       // Token 验证
}

// Handle registers a new request handle and middleware with the given path and method.
// The last handler should be the real handler, the other ones should be middleware that can and should be shared among different routes.
// See the example code in github.
//
// For GET, POST, PUT, PATCH and DELETE requests the respective shortcut
// functions can be used.
//
// This function is intended for bulk loading and to allow the usage of less
// frequently used, non-standardized or custom methods (e.g. for internal
// communication with a proxy).
func Handle(httpMethod, relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
	return engine.Handle(httpMethod, relativePath, handlers...)
}

// POST is a shortcut for router.Handle("POST", path, handle).
func POST(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
	return engine.POST(relativePath, handlers...)
}

// GET is a shortcut for router.Handle("GET", path, handle).
func GET(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
	return engine.GET(relativePath, handlers...)
}

// DELETE is a shortcut for router.Handle("DELETE", path, handle).
func DELETE(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
	return engine.DELETE(relativePath, handlers...)
}

// PATCH is a shortcut for router.Handle("PATCH", path, handle).
func PATCH(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
	return engine.PATCH(relativePath, handlers...)
}

// PUT is a shortcut for router.Handle("PUT", path, handle).
func PUT(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
	return engine.PUT(relativePath, handlers...)
}

// OPTIONS is a shortcut for router.Handle("OPTIONS", path, handle).
func OPTIONS(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
	return engine.OPTIONS(relativePath, handlers...)
}

// HEAD is a shortcut for router.Handle("HEAD", path, handle).
func HEAD(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
	return engine.HEAD(relativePath, handlers...)
}

// Any registers a route that matches all the HTTP methods.
// GET, POST, PUT, PATCH, HEAD, OPTIONS, DELETE, CONNECT, TRACE.
func Any(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
	return engine.Any(relativePath, handlers...)
}

// StaticFile registers a single route in order to serve a single file of the local filesystem.
// router.StaticFile("favicon.ico", "./resources/favicon.ico")
func StaticFile(relativePath, filepath string) gin.IRoutes {
	return engine.StaticFile(relativePath, filepath)
}

func Static(relativePath, root string) gin.IRoutes {
	return engine.Static(relativePath, root)
}

func StaticFS(relativePath string, fs http.FileSystem) gin.IRoutes {
	return engine.StaticFS(relativePath, fs)
}
func Run(addr ...string) {
	if addr == nil || len(addr) == 0 {
		port := config.GetStr("httpport")
		if len(port) > 0 {
			addr = []string{":" + port}
		}
	}
	engine.Run(addr...)
}

func Use(middleware ...gin.HandlerFunc) gin.IRoutes {
	engine.Use(middleware...)
	return engine
}

func GetUserInfo(c *gin.Context) map[string]interface{} {
	var user map[string]interface{}
	v, e := c.Get(GETUSERINFO)
	if e {
		user = v.(map[string]interface{})
	}
	return user
}
func GetUserProp(c *gin.Context, key string) (value interface{}, exists bool) {
	user := GetUserInfo(c)
	log.Println(user)
	value, exists = user[key]
	return
}

// 获取登录用户信息
func GetUserId(c *gin.Context) int64 {
	val, exist := GetUserProp(c, "user_id")

	switch val.(type) {
	case int:
		println("type:int, value:", val)
	case string:
		println("type:string, value:", val)
	default:
		println("type:unkown,value:", val)
	}
	println("userid:", val)
	if exist {
		return int64(val.(float64))
	} else {
		return 0
	}
}

// 当前账号是否超级管理员
func IsSuper(c *gin.Context) bool {
	val, exist := GetUserProp(c, "is_sup")
	if exist {
		return int(val.(float64)) == 1
	} else {
		return false
	}
}
